package com.yanqu.road.server.pb;

import com.yanqu.road.entity.activity.ActivityConditionInfo;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.UserActivityConditionData;
import com.yanqu.road.entity.activity.commonactivity.data.CommonActivityAutoBuyData;
import com.yanqu.road.entity.config.goods.MallInfo;
import com.yanqu.road.pb.common.CommonProto;
import com.yanqu.road.pb.commonactivity.CommonActivityProto;
import com.yanqu.road.server.logic.activity.common.CommonActivityData;
import com.yanqu.road.server.manger.ServerLanguageMgr;

import java.util.List;
import java.util.Map;

public class CommonActivityPb {

    private static CommonActivityProto.CommonActivityTempMsg.Builder parseCommonActivityTempMsg(ActivityInfo activityInfo, String language){
        if(null == activityInfo){
            return null;
        }
        CommonActivityProto.CommonActivityTempMsg.Builder msg = CommonActivityProto.CommonActivityTempMsg.newBuilder();
        msg.setActivityId(activityInfo.getActivityId());
        msg.setActivityName(ServerLanguageMgr.getContent(activityInfo.getActivityName(), language));
        msg.setDescription(ServerLanguageMgr.getContent(activityInfo.getActivityDesc(), language));
        msg.setResourceDir(activityInfo.getParam1());
        msg.setBeginShowTime(activityInfo.getBeginShowTime());
        msg.setEndShowTime(activityInfo.getEndShowTime());
        msg.setBeginTime(activityInfo.getBeginTime());
        msg.setEndTime(activityInfo.getEndTime());
        msg.setScoreItem(Integer.parseInt(activityInfo.getParam2()));
        msg.setActivityItem(activityInfo.getParam3());
        msg.setUseItemDesc(ServerLanguageMgr.getContent(activityInfo.getParam4(), language));
        msg.setResourceParam(activityInfo.getParam5());
        msg.setActivityType(activityInfo.getType());
        msg.setChildType(activityInfo.getChildType());
        msg.addAllServerList(activityInfo.getServerIdList());
        return msg;
    }

    public static CommonActivityProto.CommonActivityConditionTempMsg.Builder parseCommonActivityConditionTempMsg(ActivityConditionInfo activityConditionInfo, String language){
        if(null == activityConditionInfo){
            return null;
        }
        CommonActivityProto.CommonActivityConditionTempMsg.Builder msg = CommonActivityProto.CommonActivityConditionTempMsg.newBuilder();
        msg.setConditionId(activityConditionInfo.getConditionId());
        msg.setActivityId(activityConditionInfo.getActivityId());
        msg.setTitle(ServerLanguageMgr.getContent(activityConditionInfo.getTitle(), language));
        msg.setDescription(ServerLanguageMgr.getContent(activityConditionInfo.getDesc(), language));
        msg.setType(activityConditionInfo.getType());
        msg.setReward(activityConditionInfo.getRewardStr());
        msg.setMasterReward(activityConditionInfo.getMasterRewardStr());
        msg.setParamList(activityConditionInfo.getParamStr());
        msg.setDeputyMasterReward(activityConditionInfo.getDeputyMasterRewardStr());
        msg.setEliteReward(activityConditionInfo.getEliteRewardStr());
        return msg;
    }

    public static CommonActivityProto.CommonActivityConditionDataMsg.Builder parseCommonActivityConditionDataMsg(UserActivityConditionData userActivityConditionData){
        if(null == userActivityConditionData){
            return null;
        }
        CommonActivityProto.CommonActivityConditionDataMsg.Builder msg = CommonActivityProto.CommonActivityConditionDataMsg.newBuilder();
        msg.setConditionId(userActivityConditionData.getConditionId());
        msg.setValue(userActivityConditionData.getValue().toString());
        msg.setIsGetReward(userActivityConditionData.isGetReward());
        msg.setType(userActivityConditionData.getType());
        msg.setActivityId(userActivityConditionData.getActivityId());
        msg.setUnionUid(userActivityConditionData.getUnionUid());
        msg.setParams(userActivityConditionData.getParam());
        return msg;
    }

    public static CommonActivityProto.CommonActivityDataMsg.Builder parseCommonActivityDataMsg(Map<Integer, Map<Integer, CommonActivityData>> activityDataMap){
        if(null == activityDataMap){
            return null;
        }
        CommonActivityProto.CommonActivityDataMsg.Builder msg = CommonActivityProto.CommonActivityDataMsg.newBuilder();
        for(Map<Integer, CommonActivityData> tempMap : activityDataMap.values()) {
            for (CommonActivityData activityData : tempMap.values()) {
                for (UserActivityConditionData conditionData : activityData.getUserActivityConditionDataList()) {
                    CommonActivityProto.CommonActivityConditionDataMsg.Builder conditionDataMsg = parseCommonActivityConditionDataMsg(conditionData);
                    if (null != conditionDataMsg) {
                        msg.addConditionDataList(conditionDataMsg);
                    }
                }
            }
        }
        return msg;
    }

    public static CommonActivityProto.CommonActivityDataMsg.Builder parseCommonActivityDataMsg(CommonActivityData commonActivityData){
        if(null == commonActivityData){
            return null;
        }
        CommonActivityProto.CommonActivityDataMsg.Builder msg = CommonActivityProto.CommonActivityDataMsg.newBuilder();
        for (UserActivityConditionData conditionData : commonActivityData.getUserActivityConditionDataList()) {
            CommonActivityProto.CommonActivityConditionDataMsg.Builder conditionDataMsg = parseCommonActivityConditionDataMsg(conditionData);
            if (null != conditionDataMsg) {
                msg.addConditionDataList(conditionDataMsg);
            }
        }
        return msg;
    }

    public static CommonActivityProto.CommonActivityMsg.Builder parseCommonActivityMsg(ActivityInfo activityInfo,
            Map<Integer, List<ActivityConditionInfo>> activityConditionInfoMap, Map<Integer, MallInfo> activityMallInfoMap, String language){
        if(null == activityInfo) {
            return null;
        }
        CommonActivityProto.CommonActivityMsg.Builder resultMsg = CommonActivityProto.CommonActivityMsg.newBuilder();
        //活动
        CommonActivityProto.CommonActivityTempMsg.Builder activityMsg = parseCommonActivityTempMsg(activityInfo, language);
        if (null != activityMsg) {
            resultMsg.setActivityInfo(activityMsg);
        }

        //活动条件
        if(null != activityConditionInfoMap) {
            for (List<ActivityConditionInfo> conditionInfoList : activityConditionInfoMap.values()) {
                for (ActivityConditionInfo conditionInfo : conditionInfoList) {
                    CommonActivityProto.CommonActivityConditionTempMsg.Builder conditionMsg = parseCommonActivityConditionTempMsg(conditionInfo, language);
                    if (null != conditionMsg) {
                        resultMsg.addActivityConditionInfoList(conditionMsg);
                    }
                }
            }
        }

        //活动商品
        if(null != activityMallInfoMap) {
            for (MallInfo mallInfo : activityMallInfoMap.values()) {
                CommonProto.MallTempMsg.Builder mallMsg = CommonPb.parseMallTempMsg(mallInfo, language);
                if (null != mallMsg) {
                    resultMsg.addActivityMallInfoList(mallMsg);
                }
            }
        }

        return resultMsg;
    }

    public static CommonActivityProto.CommonActivityDetailMsg.Builder parseCommonActivityDetailMsg(Map<Integer, Map<Integer, CommonActivityData>> activityDataMap,
             Map<Integer, ActivityInfo> activityInfoMap, Map<Integer, Map<Integer, List<ActivityConditionInfo>>> activityConditionInfoMap,
             Map<Integer, Map<Integer, MallInfo>> activityMallInfoMap, String language){
        CommonActivityProto.CommonActivityDetailMsg.Builder resultMsg = CommonActivityProto.CommonActivityDetailMsg.newBuilder();

        for(ActivityInfo activityInfo : activityInfoMap.values()) {
            //活动条件
            Map<Integer, List<ActivityConditionInfo>> conditionInfoMap = activityConditionInfoMap.get(activityInfo.getActivityId());
            //活动商品
            Map<Integer, MallInfo> mallInfoMap = activityMallInfoMap.get(activityInfo.getActivityId());
            CommonActivityProto.CommonActivityMsg.Builder activityMsg = parseCommonActivityMsg(activityInfo, conditionInfoMap, mallInfoMap, language);
            if (null != activityMsg) {
                resultMsg.addActivityList(activityMsg);
            }
        }

        //活动数据
        CommonActivityProto.CommonActivityDataMsg.Builder activityDataMsg = parseCommonActivityDataMsg(activityDataMap);
        if(null != activityDataMsg){
            resultMsg.setActivityData(activityDataMsg);
        }

        return resultMsg;
    }

    public static CommonActivityProto.TypeCommonActivityAutoBuyData.Builder parseCommonActivityAutoBuyTypeData(CommonActivityAutoBuyData autoBuyData){
        CommonActivityProto.TypeCommonActivityAutoBuyData.Builder builder = CommonActivityProto.TypeCommonActivityAutoBuyData.newBuilder();
        builder.setChildType(autoBuyData.getChildType());
        for (Map.Entry<String,Integer> entry :autoBuyData.getChooseMap().entrySet()){
            CommonActivityProto.CommonActivityAutoBuyData.Builder dataBuild = CommonActivityProto.CommonActivityAutoBuyData.newBuilder();
            dataBuild.setItem(entry.getKey());
            dataBuild.setNum(entry.getValue());
            builder.addData(dataBuild);
        }
        return builder;
    }

}
